package com.weimei.conversionTreasure.viewModel;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.entity.SaveListBean;
import com.weimei.conversionTreasure.weight.BottomAddRecordPop;

import java.lang.reflect.Type;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CCItemVM extends ItemViewModel {

    public ObservableField< SaveListBean.DataDTO> entity = new ObservableField<>();
    SaveXlVM mSaveXlVM;

    public ObservableList<ItemItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<ItemItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_item);

    public CCItemVM(@NonNull SaveXlVM viewModel, SaveListBean.DataDTO bean) {
        super(viewModel);
        entity.set(bean);
        mSaveXlVM = (SaveXlVM) viewModel;

        String record = entity.get().getRecordArr();
        Type type = new TypeToken<List<BottomAddRecordPop.AddRecordBean>>() {}.getType();
        List<BottomAddRecordPop.AddRecordBean> list = new Gson().fromJson(record, type);
        for (BottomAddRecordPop.AddRecordBean bean1 : list) {
            ItemItemVM itemVM = new ItemItemVM(viewModel,bean1);
            observableList.add(itemVM);
        }

    }

    public BindingCommand del = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            mSaveXlVM.delXL(entity.get().getId());
        }
    });


    public BindingCommand detail = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            mSaveXlVM.showDetail(entity.get());
        }
    });

}
